package com.wfh.yudada.manager;

import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.*;
import io.reactivex.Flowable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author FengHuan Wang
 * @Date 2025/5/24 9:09
 * @Version 1.0
 */
@Component
public class AiManager {

    @Resource
    private ClientV4 client;

    public Flowable<ModelData> doAiRequest(Boolean stream, String message, float temperature) {
        ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), message);
        List<ChatMessage> messages = new ArrayList<>();
        messages.add(chatMessage);
        String requestId = String.format("YourRequestId-d%", System.currentTimeMillis());

        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model(Constants.ModelChatGLM4)
                .stream(true)
                .invokeMethod(Constants.invokeMethodAsync)
                .temperature(temperature)
                .messages(messages)
                .requestId(requestId)
                .build();
        ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
        return invokeModelApiResp.getFlowable();
    }
}
